package _rabbitmq.aliyun_endpoint;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Channel;

/**
 * @author huangya
 * @date 2024/9/11
 */
public class ProducerDemo {

    private final static String VIRTUAL_HOST = "/scene_engine_pre";
    //    private final static String VIRTUAL_HOST = "/common";
    private final static String EXCHANGE_NAME = "scene_awareness_exchange_pre";
    private final static String ROUTING_KEY = "";

    public static void main(String[] argv) throws Exception {
        // 创建连接
        ConnectionFactory factory = new ConnectionFactory();
        // RabbitMQ 服务器地址
        factory.setHost("1283899258956354.mq-amqp.cn-shanghai-a.aliyuncs.com");
        factory.setPort(5672);
        factory.setUsername("MjoxMjgzODk5MjU4OTU2MzU0OkxUQUk1dEt5SHE4bzM4cWU1dXVMYTk3dw==");
        factory.setPassword("QTEyNzdBMjg3RDE4RUFFMjJCQTU1OTIwMEZDOThCNzc5OUMxNkVGMToxNzI2Mjk2OTQwNTE5");
        factory.setVirtualHost(VIRTUAL_HOST);
        Connection connection = null;
        Channel channel = null;
        try {
            // 从连接工厂中获取连接
            connection = factory.newConnection("生产者");
            // 从连接中获取通道channel
            channel = connection.createChannel();
            // 声明一个Fanout交换机
            channel.exchangeDeclare(EXCHANGE_NAME, "fanout", true);

//            String message = "{\"vin\":\"xxxx\",\"event_name\":\"test\",\"data\":{\"key\":\"value\"}}";
            String message = "{\"data\":{\"guideData\":[{\"curFormway\":0,\"curLinkRoadClass\":10,\"routeRemainTime\":48545,\"curPointIndex\":0,\"cityCode\":0,\"viaRemainDist\":[],\"icon\":3,\"nextRouteName\":\"东华门路\",\"type\":0,\"curLinkIndex\":0,\"roadName\":\"非机动车道\",\"curSegIndex\":0,\"powerPredict\":[{\"pos\":{\"lng\":121.44505444444444,\"lat\":31.1559575},\"socPredict\":-156.59442138671875,\"milePredict\":0,\"type\":0},{\"pos\":{\"lng\":117.75597083333334,\"lat\":36.231000277777781},\"socPredict\":0,\"milePredict\":0,\"type\":3},{\"pos\":{\"lng\":117.11540027777778,\"lat\":36.842549166666664},\"socPredict\":20,\"milePredict\":0,\"type\":2}],\"routeRemainLightCount\":24,\"segRemainDist\":451,\"tunnelFlag\":0,\"routeRemainDist\":1225349,\"curRoadClass\":0,\"destDirection\":2,\"viaRemainTime\":[],\"carDirection\":0,\"pathID\":2098191063,\"straightCrossDist\":0,\"ringOutPoint\":{\"lng\":0,\"lat\":0},\"linkRemainDist\":187,\"ringInPoint\":{\"lng\":0,\"lat\":0},\"segRemainTime\":98,\"roundAboutIndex\":0,\"crossManeuverID\":3}],\"timeBar\":{\"lightBarInfo\":[{\"lightBarItems\":[{\"fineStatus\":0,\"length\":1156,\"status\":0,\"statusFlag\":3},{\"fineStatus\":170,\"length\":4287,\"status\":1,\"statusFlag\":3},{\"fineStatus\":330,\"length\":326,\"status\":3,\"statusFlag\":3},{\"fineStatus\":260,\"length\":296,\"status\":2,\"statusFlag\":3},{\"fineStatus\":170,\"length\":1028,\"status\":1,\"statusFlag\":3},{\"fineStatus\":330,\"length\":665,\"status\":3,\"statusFlag\":3},{\"fineStatus\":170,\"length\":598,\"status\":1,\"statusFlag\":3},{\"fineStatus\":330,\"length\":260,\"status\":3,\"statusFlag\":3},{\"fineStatus\":170,\"length\":930,\"status\":1,\"statusFlag\":3},{\"fineStatus\":330,\"length\":63,\"status\":3,\"statusFlag\":3},{\"fineStatus\":260,\"length\":249,\"status\":2,\"statusFlag\":3},{\"fineStatus\":330,\"length\":105,\"status\":3,\"statusFlag\":3},{\"fineStatus\":260,\"length\":264,\"status\":2,\"statusFlag\":3},{\"fineStatus\":170,\"length\":23280,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":24633,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":1008,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":8517,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":1087,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":24214,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":8747,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":14436,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":32614,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":36934,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":3608,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":9645,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":9604,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":8203,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":32718,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":6079,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":9423,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":11073,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":5315,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":70281,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":1186,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":12446,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":31749,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":22662,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":5115,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":6783,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":2688,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":7693,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":6343,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":20562,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":3006,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":9027,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":10854,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":25791,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":9136,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":4547,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":15176,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":5718,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":92191,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":12454,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":7901,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":23071,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":16871,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":5756,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":4507,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":7453,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":1534,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":32399,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":30322,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":5796,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":1600,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":5592,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":80288,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":7527,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":80129,\"status\":1,\"statusFlag\":3},{\"fineStatus\":130,\"length\":5748,\"status\":5,\"statusFlag\":3},{\"fineStatus\":170,\"length\":111962,\"status\":1,\"statusFlag\":3},{\"fineStatus\":260,\"length\":128,\"status\":2,\"statusFlag\":3},{\"fineStatus\":330,\"length\":128,\"status\":3,\"statusFlag\":3},{\"fineStatus\":260,\"length\":362,\"status\":2,\"statusFlag\":3},{\"fineStatus\":330,\"length\":1542,\"status\":3,\"statusFlag\":3},{\"fineStatus\":260,\"length\":518,\"status\":2,\"statusFlag\":3},{\"fineStatus\":170,\"length\":2187,\"status\":1,\"statusFlag\":3},{\"fineStatus\":260,\"length\":1833,\"status\":2,\"statusFlag\":3},{\"fineStatus\":170,\"length\":532,\"status\":1,\"statusFlag\":3},{\"fineStatus\":260,\"length\":1472,\"status\":2,\"statusFlag\":3},{\"fineStatus\":330,\"length\":2144,\"status\":3,\"statusFlag\":3},{\"fineStatus\":260,\"length\":320,\"status\":2,\"statusFlag\":3},{\"fineStatus\":170,\"length\":109729,\"status\":1,\"statusFlag\":3},{\"fineStatus\":260,\"length\":126,\"status\":2,\"statusFlag\":3},{\"fineStatus\":330,\"length\":412,\"status\":3,\"statusFlag\":3},{\"fineStatus\":370,\"length\":258,\"status\":4,\"statusFlag\":3},{\"fineStatus\":330,\"length\":696,\"status\":3,\"statusFlag\":3},{\"fineStatus\":170,\"length\":384,\"status\":1,\"statusFlag\":3},{\"fineStatus\":260,\"length\":384,\"status\":2,\"statusFlag\":3},{\"fineStatus\":170,\"length\":5499,\"status\":1,\"statusFlag\":3},{\"fineStatus\":260,\"length\":282,\"status\":2,\"statusFlag\":3},{\"fineStatus\":170,\"length\":2018,\"status\":1,\"statusFlag\":3},{\"fineStatus\":260,\"length\":128,\"status\":2,\"statusFlag\":3},{\"fineStatus\":330,\"length\":628,\"status\":3,\"statusFlag\":3},{\"fineStatus\":370,\"length\":1292,\"status\":4,\"statusFlag\":3},{\"fineStatus\":330,\"length\":577,\"status\":3,\"statusFlag\":3},{\"fineStatus\":260,\"length\":410,\"status\":2,\"statusFlag\":3},{\"fineStatus\":330,\"length\":1306,\"status\":3,\"statusFlag\":3},{\"fineStatus\":260,\"length\":704,\"status\":2,\"statusFlag\":3},{\"fineStatus\":170,\"length\":1718,\"status\":1,\"statusFlag\":3},{\"fineStatus\":330,\"length\":247,\"status\":3,\"statusFlag\":3},{\"fineStatus\":170,\"length\":1130,\"status\":1,\"statusFlag\":3},{\"fineStatus\":330,\"length\":278,\"status\":3,\"statusFlag\":3},{\"fineStatus\":170,\"length\":679,\"status\":1,\"statusFlag\":3},{\"fineStatus\":0,\"length\":69,\"status\":0,\"statusFlag\":3}]}]}},\"sceneId\":\"parking_adjust_view\",\"event_name\":\"parking_adjust_view\",\"vin\":\"LSJA24396BS300004\"}";
            // 发布消息到交换机
            channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, message.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + message + "'");
        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("发送消息出现异常...");
        } finally {
            // 7: 释放连接关闭通道
            if (channel != null && channel.isOpen()) {
                try {
                    channel.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (connection != null && connection.isOpen()) {
                try {
                    connection.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
